package com.andyliu.system.controller;


import com.andyliu.common.ResponseResult;
import com.andyliu.controller.BaseController;
import com.andyliu.interceptor.BusinessType;
import com.andyliu.log.Log;
import com.andyliu.system.dto.MenuDto;
import com.andyliu.system.entity.Menu;
import com.andyliu.system.entity.Role;
import com.andyliu.system.service.IMenuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author andyLiu
 * @since 2021-08-10
 */
@Slf4j
@Controller
@RequestMapping("/system/menu")
@Api(value = "菜单权限表控制器",tags = "菜单权限表操作接口")
    public class MenuController extends BaseController {
    @Autowired
    private IMenuService service;
    /**
    * 跳转到列表页
    */
    @GetMapping()
    @ApiIgnore
    public String toData(){
        log.debug("跳转到列表页");
        return"system/Menu/data";
    }
    /**
    * 异步加载分页数据
    */
    @PostMapping("data")
    @ResponseBody
    @ApiOperation(value = "菜单权限表分页数据接口",notes = "根据查询和分页条件返回分页数据",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber",value = "分页页码",required = true,type = "Integer"),
            @ApiImplicitParam(name = "pageSize",value = "每页显示个数",required = true,type = "Integer"),
            @ApiImplicitParam(name="obj",value="用于分装实体的查询条件",required = false)
    })
    @ApiResponses({ @ApiResponse(code = 200, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常"),
            @ApiResponse(code = 501, message = "权限不足")
    })
    @Log(moduleName = "菜单查询",businessType = BusinessType.QUERY)
    public ResponseResult data(Menu obj){
            log.debug("分页查询Menu数据");
        QueryWrapper<Menu> param = new QueryWrapper<>();
        if(obj!=null){
            if(StringUtils.isNotEmpty(obj.getMenuName())){
                param.like("menu_name","%"+obj.getMenuName()+"%");
            }
            if(StringUtils.isNotEmpty(obj.getMenuType())){
                param.eq("menu_type",obj.getMenuType());
            }
        }
        List<Menu> list = service.list(param);
        return ResponseResult.success(list);
    }
    /**
    * 跳转到新增编辑页面
    */
    @GetMapping("/edit/{id}")
    @ApiIgnore
    public String toEidt(@PathVariable Integer id,ModelMap mmap){
        if(id!=-1){
            Menu menu=service.getById(id);
            mmap.put("menu" ,menu);
        }
        return"system/Menu/edit";
    }
    @GetMapping("/info/{id}")
    @ResponseBody
    @ApiOperation(value = "根据主键获取菜单权限表数据接口",notes = "根据主键获取实体数据",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "主键",required = true,type = "Integer")
    })
    @ApiResponses({ @ApiResponse(code = 200, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常"),
            @ApiResponse(code = 501, message = "权限不足")
    })
    @Log(moduleName = "按主键查询",businessType = BusinessType.QUERY)
    public ResponseResult toEidt(@PathVariable Integer id){
        log.debug("查看菜单权限表的数据,id为"+id);
        MenuDto menu = service.getById(id);
        return ResponseResult.success(menu);
    }

    /**
    * 异步保存或新增
    */
    @PostMapping("save")
    @ResponseBody
    @ApiOperation(value = "保存或更新菜单权限表数据接口",notes = "如果实体类中有主键为更新，否则为保存",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user",value = "用户信息表实体",required = true,type = "Integer")
    })
    @ApiResponses({ @ApiResponse(code = 200, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常"),
            @ApiResponse(code = 501, message = "权限不足")
    })
    @Log(moduleName = "保存菜单",businessType = BusinessType.INSERT)
    public ResponseResult save(@RequestBody  Menu menu){
        log.debug("保存菜单权限表的数据");
        if(menu.getMenuId()!=null){
            boolean status=service.updateById(menu);
            if(status){
            return ResponseResult.success();
            }else{
            return ResponseResult.error();
            }
        }
        return service.save(menu)==true?
        ResponseResult.success()
        :ResponseResult.error();
    }
    /**
    * 删除或批量删除请求
    */
    @DeleteMapping("delete")
    @ResponseBody
    @ApiOperation(value = "批量删除菜单权限表数据接口",notes = "批量删除",httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids",value = "需要删除的主键的数组",required = true,type = "Integer")
    })
    @ApiResponses({ @ApiResponse(code = 200, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常"),
            @ApiResponse(code = 501, message = "权限不足")
    })
    @Log(moduleName = "菜单删除",businessType = BusinessType.DELETE)
    public boolean delete(Integer[] ids){
        log.debug("删除菜单权限表的数据,id为"+ids);
        return service.removeByIds(Arrays.asList(ids));
    }
    /**
    * 单个删除
    */
    @DeleteMapping("delete/{id}")
    @ResponseBody
    @ApiOperation(value = "删除菜单权限表数据接口",notes = "单个删除，根据主键删除",httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "需要删除主键",required = true,type = "Integer")
    })
    @ApiResponses({ @ApiResponse(code = 200, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常"),
            @ApiResponse(code = 501, message = "权限不足")
    })
    @Log(moduleName = "菜单删除",businessType = BusinessType.DELETE)
    public ResponseResult delete(@PathVariable Integer id){
        log.debug("删除菜单权限表的数据,id为"+id);
        boolean status =  service.removeById(id);
        if(status){
            return ResponseResult.success("删除成功");
        }else{
            return ResponseResult.error();
        }
    }


    /**
     * 加载所有菜单列表树
     */
    @GetMapping("/menuTreeData")
    @ResponseBody
    public List<MenuDto> menuTreeData(Role role)
    {
        List<MenuDto> list = service.listRoleMenus(role.getRoleId().intValue());
        return list;
    }

    @GetMapping("toTree")
    public String toTree(){
        return "system/Menu/tree";
    }

    @PostMapping("/saveRoleMenus")
    @ResponseBody
    public ResponseResult saveRoleMenus(@RequestParam(name = "ids[]") Integer[] ids,Integer roleId){
        boolean status = service.saveRoleMenus(roleId,ids);
        if(status) return ResponseResult.success();
        return ResponseResult.error();
    }

    @GetMapping("/menuForIndex")
    @ResponseBody
    public ResponseResult menuForIndex(Role role)
    {
        List<MenuDto> list = service.listRoleMenusForIndex(role.getRoleId().intValue());
        return ResponseResult.success(list);
    }

}


